package com.wj.rspmproperty.controller;


import com.wj.rspmproperty.pojo.PageResult;
import com.wj.rspmproperty.pojo.ResponsePojo;
import com.wj.rspmproperty.pojo.User;
import com.wj.rspmproperty.pojo.vo.UnpaidOwner;
import com.wj.rspmproperty.service.UserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.MediaType;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.nio.charset.StandardCharsets;
import java.util.Optional;

/**
 * @author WeiJin
 * @version 1.0
 */

@RestController
@RequestMapping("/user")
@Api(tags = "业主相关接口")
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 新增业主
     *
     * @param user
     * @return
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增业主信息，默认未激活状态，采用默认密码，生成随机账号。roomid通过级联菜单获得",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "idcard", type = "String", required = true),
            @ApiImplicitParam(name = "age", type = "Integer", required = true),
            @ApiImplicitParam(name = "gender", type = "Integer", required = true),
            @ApiImplicitParam(name = "username", type = "String", required = true),
            @ApiImplicitParam(name = "user_roomid", type = "Integer", required = true)
    })
    public ResponsePojo<Integer> insertUser(@ApiIgnore @RequestBody User user) {
        user.setPassword("123456");
        //用户账号
        String name = DigestUtils.md5DigestAsHex(user.getUsername().getBytes(StandardCharsets.UTF_8)).substring(13, 20);
        name = name.concat(System.currentTimeMillis() % 10000 + "");
        user.setAccount(name);
        //用户类型--业主
        user.setUser_type((short) 3);
        //用户状态--未激活
        user.setUser_status((short) 33);

        Integer result = null;
        try {
            result = userService.insertUser(user);
            return ResponsePojo.success(result);
        } catch (DuplicateKeyException e) {
            e.printStackTrace();
            return ResponsePojo.fail(result, "用户名重复");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponsePojo.fail(result, "未知异常，请联系管理员");
        }
    }

    /**
     * 分页查询业主信息
     *
     * @param current 第几页
     * @param size    每页多少行
     * @return
     */

    @GetMapping("/pageOfOwner")
    @ApiOperation(value = "分页查询业主信息，每页十条记录，默认第一页", notes = "当前页数小于1，默认第1页；每页记录小于10，默认10")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", defaultValue = "1"),
            @ApiImplicitParam(name = "size", defaultValue = "10")
    })
    public ResponsePojo<PageResult<User>> pageOfOwners(@RequestParam(defaultValue = "1") Integer current,
                                                       @RequestParam(defaultValue = "10") Integer size) {

        PageResult<User> result = userService.pageOfOwners(current, size);
        return ResponsePojo.success(result);
    }

    /**
     * 修改业主状态
     * @param user
     * @return
     */
    @PutMapping("/updateStatus/{userid}")
    @ApiOperation(value = "修改用户状态，主要是更换业主后", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid",type = "Integer",required = true,paramType = "path"),
            @ApiImplicitParam(name = "user_status",type = "Integer",required = true,paramType = "query")
    })
    public ResponsePojo<Integer> changeUserStatus(@ApiIgnore @RequestBody User user) {
        int result = userService.changeUserStatus(user);
        if (result == 0){
            return ResponsePojo.fail(result,"该id失效");
        }
        return ResponsePojo.success(result);
    }


    /**
     * 根据id查询用户详细信息
     * @param id
     * @return
     */
    @GetMapping("/id/{id}")
    @ApiOperation(value = "根据用户id查询用户详细信息",notes = "<span style='color:red'>若查询失败，返回null</span>",
        produces = MediaType.APPLICATION_JSON_VALUE
    )
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id",type = "Integer",required = true)
    )
    public ResponsePojo<User> queryUserById(@PathVariable("id") Integer id){
        User user = userService.queryById(id);
        if (Optional.ofNullable(user).isPresent()){
            return ResponsePojo.success(user);
        }
        return ResponsePojo.fail(user,"该id不存在");
    }


    /**
     * 分页查询欠费用户信息
     * @param current
     * @param size
     * @param typeId 欠费类型
     * @return
     */
    @GetMapping("/unpaidOwnerList")
    @ApiOperation(value = "单项费用的欠费名单查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",defaultValue = "1"),
            @ApiImplicitParam(name = "size",defaultValue = "10"),
            @ApiImplicitParam(name = "typeId",required = true)
    })
    public ResponsePojo<PageResult<UnpaidOwner>> unpaidOwnerList(
            @RequestParam(defaultValue = "1")Integer current,
            @RequestParam(defaultValue = "10")Integer size,
            Integer typeId){
        return ResponsePojo.success(userService.pageOfUnpaidOwnerList(current,size,typeId));
    }
}
